﻿using Microsoft.VisualStudio.TemplateWizard;
using System.Collections.Generic;

namespace Caffeine.CaffeineModelItemTemplate.Wizard
{
    public class CaffeineModelItemTemplateWizard : IWizard
    {
        public CaffeineModelItemTemplateWizard()
        {
            ChildrenProjectItems = new List<EnvDTE.ProjectItem>();
        }

        public void BeforeOpeningFile(EnvDTE.ProjectItem projectItem)
        {
        }

        public void ProjectFinishedGenerating(EnvDTE.Project project)
        {
        }

        public void ProjectItemFinishedGenerating(EnvDTE.ProjectItem projectItem)
        {
            string projectItemName = projectItem.Name.ToUpper();

            if(projectItemName.IndexOf(".MODEL.CS") > 0)
            {
                ParentProjectItem = projectItem;
            }
            else if(projectItemName.IndexOf(".COMMANDS.CS") > 0)
            {
                ChildrenProjectItems.Add(projectItem);
            }
            else if(projectItemName.IndexOf(".PROPERTIES.CS") > 0)
            {
                ChildrenProjectItems.Add(projectItem);
            }
            else if(projectItemName.IndexOf(".VALIDATIONS.CS") > 0)
            {
                ChildrenProjectItems.Add(projectItem);
            }
            else
            {
                //Note a file we care about
            }
        }

        public void RunFinished()
        {
            foreach (EnvDTE.ProjectItem item in ChildrenProjectItems)
            {
                string filename = item.get_FileNames(0);
                ParentProjectItem.ProjectItems.AddFromFile(filename);
            }
        }

        public void RunStarted
        (
            object automationObject, 
            Dictionary<string, string> replacementsDictionary, 
            WizardRunKind runKind, 
            object[] customParams
        )
        {
        }

        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }

        private EnvDTE.ProjectItem ParentProjectItem
        {
            get;
            set;
        }

        private List<EnvDTE.ProjectItem> ChildrenProjectItems
        {
            get;
            set;
        }
    }
}